<template>

  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-tabs v-model="activeName" center>
          <!-- 登录账户设置--------------------------------------------------------------- -->
          <el-tab-pane label="登录账户设置" name="first">

            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" width="120px" label-width="120px" class="demo-ruleForm">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="ruleForm.username" class="inpu" />
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" class="inpu" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 个人详情--------------------------------------------------------------- -->
          <el-tab-pane label="个人详情" width="120px" name="second" center>
            <el-form ref="form" :model="form" :rules="rules" width="120px" label-width="120px" class="demo-ruleForm">
              <el-row type="flex">
                <el-col>

                  <el-form-item label="工号" prop="workNumber">
                    <el-input v-model="form.workNumber" class="inpu" />
                  </el-form-item>

                  <el-form-item label="姓名" prop="username">
                    <el-input v-model="form.username" class="inpu" />
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" class="inpu" type="password" />
                  </el-form-item>
                  <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="form.mobile" class="inpu" />
                  </el-form-item>
                  <el-form-item label="头像设置">
                    <Upload ref="Uplad" @lod="lodFn" />
                  </el-form-item>
                </el-col>
                <el-col>
                  <el-form-item label="入职时间" prop="correctionTime">
                    <el-date-picker

                      v-model="form.correctionTime"

                      type="date"
                      placeholder="选择日期"
                      class="inpu"
                    />

                  </el-form-item>
                  <el-form-item label="聘用形式">
                    <el-select v-model="form.companyId " class="inpu" placeholder="请选择活动区域">
                      <el-option v-for="obj in constant.hireType" :key="obj.id" :label="obj.value" :value="obj.id" />

                    </el-select>
                  </el-form-item>
                  <el-form-item label="部门" prop="departmentName">
                    <el-input v-model="form.departmentName" class="inpu" />
                  </el-form-item>
                </el-col>

              </el-row>

              <el-form-item>
                <el-button type="primary" @click="setsubmitForm('form')">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 岗位信息--------------------------------------------------------------- -->
          <el-tab-pane label="岗位信息" name="third">
            1
          </el-tab-pane>

        </el-tabs>

      </div>

    </el-card>

  </div>
</template>

<script>
import { PersonalInformationAPI, UpdateEmployeeInformationAPI } from '@/api/employeesAPI'
import constant from '@/constant/employees'
import Upload from '@/views/Upload'

export default {
  name: 'LookEmployees',
  components: {
    Upload
  },
  data() {
    return {
      constant: constant,
      isShow: '',

      activeName: 'first',
      ruleForm: {
        username: '',
        password: ''

      },
      form: {
        username: '',
        password: '',
        correctionTime: '',
        staffPhoto: '',
        companyId: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],

        companyId: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请选择工号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.PersonalInformation(this.$route.query.id)
    console.log(this.$refs.Uplad)
  },
  methods: {
    // 获取员基本信息
    async PersonalInformation(id) {
      const res = await PersonalInformationAPI(id)
      console.log(res)
      this.ruleForm = res.data
      this.$refs.Uplad.staffPhotoUrl = res.data.staffPhoto
      this.form = res.data
      this.form.companyId = this.form.companyId === '1' ? '正式' : '非正式'
    },
    // 保存员工信息
    submitForm(formName, data) {
      console.log(this.$refs.Uplad.staffPhotoUrl)

      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          await UpdateEmployeeInformationAPI(this.$route.query.id, data || this.ruleForm)
          this.$message.success('保存成功')
          this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 保存员工
    setsubmitForm(formName) {
      if (this.isShow === '100' || this.isShow === '0') return
      this.form.staffPhoto = this.$refs.Uplad.staffPhotoUrl
      this.submitForm(formName, this.form)
    },
    // 照片验证
    lodFn(a) {
      this.isShow = a
    }
  }
}
</script>

<style>
.app-main{
  padding: 40px;
}
.inpu{
  width: 320px;
}
</style>
